/*
搓牌界面的处理逻辑，雀九模式和三公模式的搓牌有区别，做了不同的逻辑处理
*/
var confige = require("confige");//195*468
cc.Class({
    extends: cc.Component,

    properties: {
        parent:{
            default:null,
            type:cc.Node
        },
        isInit:false,
    },

    onLoad: function () {

    },

    onInit:function(type){
        if(type == "bankerMode")
        {
            this.initBankerMode();
            return;
        }
        this.showCardFunc = -1;
        this.showCardFunc2 = -1;
        this.moveBeginPosX = -400;
        this.moveBeginPos1 = 390;
        this.moveDistance1 = -155;
        this.moveBeginPos2 = -900;
        this.moveDistance2 = 400;

        this.btnOpenCard = this.node.getChildByName("btnOpenCard");
        this.mask = this.node.getChildByName("mask");
        this.cardListNode = this.node.getChildByName("cardList");
        this.cardList = {};
        for(var i=0;i<5;i++){
            this.cardList[i] = this.cardListNode.getChildByName("card"+i);
            this.cardList[i].zIndex = 5-i;
        }
        this.curCardNum = 0;
        this.canMove = true;

        this.selectZ = 10;
        this.selectIndex = -1;
        this.selectOriPos = {x:0,y:0};
        this.moveOriPos = {x:0,y:0};

        this.resetCard();
        var self = this;
        var moveStart0 = function(touches){
            self.moveOriPos = {x:touches.getLocationX(),y:touches.getLocationY()};
            self.selectIndex = 0;
            self.selectOriPos = {x:self.cardList[self.selectIndex].x,y:self.cardList[self.selectIndex].y};
        };
        var moveStart1 = function(touches){
            self.moveOriPos = {x:touches.getLocationX(),y:touches.getLocationY()};
            self.selectIndex = 1;
            self.selectOriPos = {x:self.cardList[self.selectIndex].x,y:self.cardList[self.selectIndex].y};
        };
        var moveStart2 = function(touches){
            self.moveOriPos = {x:touches.getLocationX(),y:touches.getLocationY()};
            self.selectIndex = 2;
            self.selectOriPos = {x:self.cardList[self.selectIndex].x,y:self.cardList[self.selectIndex].y};
        };
        var moveStart3 = function(touches){
            self.moveOriPos = {x:touches.getLocationX(),y:touches.getLocationY()};
            self.selectIndex = 3;
            self.selectOriPos = {x:self.cardList[self.selectIndex].x,y:self.cardList[self.selectIndex].y};
        };
        var moveStart4 = function(touches){
            self.moveOriPos = {x:touches.getLocationX(),y:touches.getLocationY()};
            self.selectIndex = 4;
            self.selectOriPos = {x:self.cardList[self.selectIndex].x,y:self.cardList[self.selectIndex].y};
        };

        var touchMove = function(touches){
            if(self.selectIndex != -1 && self.canMove)
            {
                var newPosX = self.selectOriPos.x + (touches.getLocationX() - self.moveOriPos.x);
                var newPosY = self.selectOriPos.y + (touches.getLocationY() - self.moveOriPos.y);
                self.cardList[self.selectIndex].x = newPosX;
                self.cardList[self.selectIndex].y = newPosY;
            }
        };
        var touchEnd = function(touches){
            if(self.canMove)
                self.cancelMove();
        };
        var touchCancel = function(touches){
            if(self.canMove)
                self.cancelMove();
        };
        this.cardList[0].on(cc.Node.EventType.TOUCH_START, moveStart0, self);
        this.cardList[1].on(cc.Node.EventType.TOUCH_START, moveStart1, self);
        this.cardList[2].on(cc.Node.EventType.TOUCH_START, moveStart2, self);
        this.cardList[3].on(cc.Node.EventType.TOUCH_START, moveStart3, self);
        this.cardList[4].on(cc.Node.EventType.TOUCH_START, moveStart4, self);
        for(var i=0;i<5;i++)
        {
            this.cardList[i].on(cc.Node.EventType.TOUCH_MOVE, touchMove, self);
            this.cardList[i].on(cc.Node.EventType.TOUCH_END, touchEnd, self); 
            this.cardList[i].on(cc.Node.EventType.TOUCH_CANCEL, touchCancel, self);
        }
        
        this.isInit = true;
    },

    cancelMove:function(){
        this.selectIndex = -1;
        var checkDis = false;
        console.log("curCardNum====="+this.curCardNum);
        if(Math.abs(this.cardList[this.curCardNum-1].x-this.cardList[this.curCardNum-2].x)>50 || Math.abs(this.cardList[this.curCardNum-1].y-this.cardList[this.curCardNum-2].y)>50)
            checkDis = true;
        if(checkDis)
        {
            console.log("搓牌成功")
            if(this.canMove)
            {
                // if(this.showCardFunc2 != -1)
                    // this.unschedule(this.showCardFunc2);
                window.protectedUnschedule(this,this.showCardFunc2);
                this.btnOpenCard.active = false;
                this.showCardFunc2 = function(){
                    this.btnShowCardClick();
                };
                this.scheduleOnce(this.showCardFunc2,1);
            }
            this.canMove = false;
        }else{
            console.log("搓牌失败,距离再拉大一些");
        }
    },

    resetCard:function(){
        for(var i=0;i<5;i++)
        {
            this.cardList[i].stopAllActions();
            this.cardList[i].x = 0;
            this.cardList[i].y = -140;
            this.cardList[i].scale = 0.33;
            this.cardList[i].rotation = 0;
            this.cardList[i].active = false;
            this.cardList[i].zIndex = 5-i;
        }
        this.mask.stopAllActions();
        this.curCardNum = 0;
        this.curCardIndex = -1;
        this.canMove = true;
        this.selectZ = 10;
        this.mask.opacity = 0;
        this.btnOpenCard.active = true;
    },

    initCard:function(handCard){
        this.canMove = true;
        this.mask.runAction(cc.fadeTo(0.3,150));
        for(var i in handCard)
        {
            this.curCardNum++;
            this.cardList[i].active = true;
            var curCardIndex = handCard[i].num + handCard[i].type*13;
            this.cardList[i].getComponent("cc.Sprite").spriteFrame = confige.bigCardFrameMap[curCardIndex];
        }

        if(this.curCardNum == 5)
        {
            this.cardList[0].y = this.moveBeginPos1;
            this.cardList[1].y = this.moveBeginPos1;
            
            this.cardList[0].x = this.moveBeginPosX;
            this.cardList[1].x = this.moveBeginPosX+80;

            this.cardList[2].y = this.moveBeginPos2;
            this.cardList[3].y = this.moveBeginPos2;
            this.cardList[4].y = this.moveBeginPos2;

            this.cardList[2].scale = 1.5;
            this.cardList[3].scale = 1.5;
            this.cardList[4].scale = 1.5;
            // this.cardList[2].rotation = 4;
            // this.cardList[4].rotation = -4;

            this.cardList[0].runAction(this.getAction(0));
            this.cardList[1].runAction(this.getAction(0));
            this.cardList[2].runAction(cc.sequence(this.getAction(1),cc.rotateTo(0.2,2)));
            this.cardList[3].runAction(this.getAction(1));
            this.cardList[4].runAction(cc.sequence(this.getAction(1),cc.rotateTo(0.2,-2)));
        }
        if(this.curCardNum == 3)
        {
            this.cardList[0].y = this.moveBeginPos2;
            this.cardList[1].y = this.moveBeginPos2;
            this.cardList[2].y = this.moveBeginPos2;

            this.cardList[0].scale = 1.7;
            this.cardList[1].scale = 1.7;
            this.cardList[2].scale = 1.7;
            // this.cardList[0].rotation = 4;
            // this.cardList[2].rotation = -4;

            this.cardList[0].runAction(cc.sequence(this.getAction(1),cc.rotateTo(0.2,2)));
            this.cardList[1].runAction(this.getAction(1));
            this.cardList[2].runAction(cc.sequence(this.getAction(1),cc.rotateTo(0.2,-2)));
        }
    },

    btnShowCardClick:function(){
        window.DKTools.audioEG.playEffect("tips_btnClick");
        this.canMove = false;
        // if(this.showCardFunc != -1)
            // this.unschedule(this.showCardFunc);
        window.protectedUnschedule(this,this.showCardFunc);
        this.btnOpenCard.active = false;
        for(var i=0;i<5;i++){
            this.cardList[i].stopAllActions();
            this.cardList[i].zIndex = i;
        }
        this.mask.stopAllActions();
        if(this.curCardNum == 5)
        {
            this.cardList[0].runAction(this.getAction(2));
            this.cardList[1].runAction(this.getAction(2));

            this.cardList[2].runAction(cc.sequence(this.getAction(3),cc.rotateTo(0.2,-10),cc.delayTime(0.2),this.getAction(4)));
            this.cardList[3].runAction(cc.sequence(this.getAction(3),cc.rotateTo(0.2,0),cc.delayTime(0.2),this.getAction(4)));
            this.cardList[4].runAction(cc.sequence(this.getAction(3),cc.rotateTo(0.2,10),cc.delayTime(0.2),this.getAction(4)));
            this.mask.runAction(cc.sequence(cc.delayTime(0.5),cc.fadeOut(0.75)));
        }
        if(this.curCardNum == 3)
        {
            this.cardList[0].runAction(cc.sequence(this.getAction(3),cc.rotateTo(0.2,-10),cc.delayTime(0.2),this.getAction(4)));
            this.cardList[1].runAction(cc.sequence(this.getAction(3),cc.rotateTo(0.2,0),cc.delayTime(0.2),this.getAction(4)));
            this.cardList[2].runAction(cc.sequence(this.getAction(3),cc.rotateTo(0.2,10),cc.delayTime(0.2),this.getAction(4)));
            this.mask.runAction(cc.sequence(cc.delayTime(0.5),cc.fadeOut(0.75)));
        }
        this.showCardFunc = function(){
            this.parent.btnShowCardOnMove();
        };
        this.scheduleOnce(this.showCardFunc,1.3);
    },

    showLayer:function(handCard){//moveType:0:5张牌,明3张;1:5张牌,明4张;2:3张牌,明2张;3:3张牌,明0张;
        if(this.isInit == false)
            this.onInit();
        // if(this.showCardFunc != -1)
            // this.unschedule(this.showCardFunc);
        // if(this.showCardFunc2 != -1)
            // this.unschedule(this.showCardFunc2);
        window.protectedUnschedule(this,this.showCardFunc);
        window.protectedUnschedule(this,this.showCardFunc2);
        this.initCard(handCard);
        this.node.active = true;
    },

    hideLayer:function(){
        this.resetCard();
        this.node.active = false;
    },

    getAction:function(actionIndex){
        switch(actionIndex)
        {
            case 0:
            return cc.moveBy(0.5,cc.p(0,this.moveDistance1)).easing(cc.easeOut(2.2));
            break;
            case 1:
            return cc.moveBy(0.5,cc.p(0,this.moveDistance2)).easing(cc.easeOut(2.2));
            break;
            case 2:
            return cc.sequence(cc.delayTime(0.75),cc.moveBy(0.5,cc.p(0,-this.moveDistance1)).easing(cc.easeOut(2.2)));
            break;
            case 3:
            return cc.moveTo(0.3,cc.p(0,this.moveBeginPos2+this.moveDistance2));
            break;
            case 4:
            return cc.moveTo(0.5,cc.p(0,this.moveBeginPos2-50)).easing(cc.easeOut(2));
            break;
            default:
            break;
        }
    },

    initBankerMode:function(){
        this.checkCard1 = false;
        this.checkCard2 = false;
        this.showCardFunc = -1;
        this.showCardFunc2 = -1;
        this.cardPos1 = {x:-130,y:0};
        this.cardPos2 = {x:130,y:0};

        this.btnOpenCard = this.node.getChildByName("btnOpenCard");
        this.mask = this.node.getChildByName("mask");
        this.cardListNode = this.node.getChildByName("cardList");
        this.cardList = {};
        for(var i=0;i<2;i++)
            this.cardList[i] = this.cardListNode.getChildByName("card"+i);
        
        this.canMove = true;
        this.selectOriPos1 = {x:0,y:0};
        this.moveOriPos1 = {x:0,y:0};
        this.selectOriPos2 = {x:0,y:0};
        this.moveOriPos2 = {x:0,y:0};

        this.maskNode1 = this.node.getChildByName("maskNode1");
        this.maskNode2 = this.node.getChildByName("maskNode2");
        this.maskNodeWidth1 = this.maskNode1.width;
        this.maskNodeHeight1 = this.maskNode1.height;
        this.maskNodeWidth2 = this.maskNode2.width;
        this.maskNodeHeight2 = this.maskNode2.height;

        this.resetCardB();
        var self = this;
        var moveStart1 = function(touches){
            self.moveOriPos1 = {x:touches.getLocationX(),y:touches.getLocationY()};
            self.selectIndex = 1;
            self.selectOriPos1 = {x:self.maskNode1.x,y:self.maskNode1.y};
        };
        var moveStart2 = function(touches){
            self.moveOriPos2 = {x:touches.getLocationX(),y:touches.getLocationY()};
            self.selectIndex = 2;
            self.selectOriPos2 = {x:self.maskNode2.x,y:self.maskNode2.y};
        };

        var touchMove1 = function(touches){
            if(self.selectIndex != -1 && self.canMove)
            {
                var newPosX = self.selectOriPos1.x + (touches.getLocationX() - self.moveOriPos1.x);
                var newPosY = self.selectOriPos1.y + (touches.getLocationY() - self.moveOriPos1.y);
                self.maskNode1.x = newPosX;
                self.maskNode1.y = newPosY;
            }
        };
        var touchMove2 = function(touches){
            if(self.selectIndex != -1 && self.canMove)
            {
                var newPosX = self.selectOriPos2.x + (touches.getLocationX() - self.moveOriPos2.x);
                var newPosY = self.selectOriPos2.y + (touches.getLocationY() - self.moveOriPos2.y);
                self.maskNode2.x = newPosX;
                self.maskNode2.y = newPosY;
            }
        };
        var touchEnd1 = function(touches){
            if(self.canMove)
                self.cancelMoveB(1);
        };
        var touchCancel1 = function(touches){
            if(self.canMove)
                self.cancelMoveB(1);
        };
        var touchEnd2 = function(touches){
            if(self.canMove)
                self.cancelMoveB(2);
        };
        var touchCancel2 = function(touches){
            if(self.canMove)
                self.cancelMoveB(2);
        };
        this.maskNodeBtn1 = this.maskNode1.getChildByName("btn");
        this.maskNodeBtn2 = this.maskNode2.getChildByName("btn");
        this.maskNodeBtn1.on(cc.Node.EventType.TOUCH_START, moveStart1, self);
        this.maskNodeBtn1.on(cc.Node.EventType.TOUCH_MOVE, touchMove1, self);
        this.maskNodeBtn1.on(cc.Node.EventType.TOUCH_END, touchEnd1, self); 
        this.maskNodeBtn1.on(cc.Node.EventType.TOUCH_CANCEL, touchCancel1, self);
        this.maskNodeBtn2.on(cc.Node.EventType.TOUCH_START, moveStart2, self);
        this.maskNodeBtn2.on(cc.Node.EventType.TOUCH_MOVE, touchMove2, self);
        this.maskNodeBtn2.on(cc.Node.EventType.TOUCH_END, touchEnd2, self); 
        this.maskNodeBtn2.on(cc.Node.EventType.TOUCH_CANCEL, touchCancel2, self);
        
        this.isInit = true;

        // var self = this;
        // cc.eventManager.addListener({
        //     event: cc.EventListener.TOUCH_ALL_AT_ONCE,
        //     onTouchesBegan: function (touches, event) {
        //         console.log(touches);
        //         return true;
        //     },
        //     onTouchesMoved: function (touches, event) {
        //         console.log(touches);
        //         // if (touches.length >= 2) {
        //         //     // var touch1 = touches[0], touch2 = touches[1];
                    
        //         //     // self.touch1.string = "X = " + touch1.getLocation().x.toFixed(2) + "---Y = " + touch1.getLocation().y.toFixed(2);
        //         //     // self.touch2.string = "X = " + touch2.getLocation().x.toFixed(2) + "---Y = " + touch2.getLocation().y.toFixed(2);
        //         //     // var delta1 = touch1.getDelta(), delta2 = touch2.getDelta();
        //         //     // var touchPoint1 = parent.convertToNodeSpaceAR(touch1.getLocation());
        //         //     // var touchPoint2 = parent.convertToNodeSpaceAR(touch2.getLocation());
        //         //     // //缩放
        //         //     // var distance = touchPoint1.sub(touchPoint2);
        //         //     // var delta = delta1.sub(delta2);
        //         //     // var scale = 1;
        //         //     // if (Math.abs(distance.x) > Math.abs(distance.y)) {
        //         //     //     scale = (distance.x + delta.x) / distance.x * self.target.scale;
        //         //     // }
        //         //     // else {
        //         //     //     scale = (distance.y + delta.y) / distance.y * self.target.scale;
        //         //     // }
        //         //     // self.target.scale = scale < 0.5 ? 0.5 : scale;
        //         // }
        //     }
        // }, self.node);
    },

    cancelMoveB:function(index){
        //x:70;y:150
        //check card1
        this.selectIndex = index;
        if(this.selectIndex == 1)
        {
            console.log("cancelMoveB11111====");
            console.log(this.maskNode1.x);
            console.log(this.cardList[0].x)
            console.log(this.maskNode1.x-this.cardList[0].x);
            console.log(this.maskNode1.y-this.cardList[0].y);
            if(this.maskNode1.x-this.cardList[0].x < -110 || this.maskNode1.x-this.cardList[0].x > 330){
                console.log("this.checkCard1 = true!!!!!!!");
                this.checkCard1 = true;
                this.maskNode1.active = false
            }else{
                this.checkCard1 = false;
            }
            if(this.maskNode1.y-this.cardList[0].y < -230 || this.maskNode1.y-this.cardList[0].y > 230){
                console.log("this.checkCard1 = true!!!!!!!");
                this.checkCard1 = true;
                this.maskNode1.active = false;
            }
        }
        //check card2
        if(this.selectIndex == 2)
        {
            console.log("cancelMoveB22222====");
            console.log(this.maskNode2.x);
            console.log(this.cardList[1].x)
            console.log(this.maskNode2.x-this.cardList[1].x);
            console.log(this.maskNode2.y-this.cardList[1].y);
            if(this.maskNode2.x-this.cardList[1].x < -330 || this.maskNode2.x-this.cardList[1].x > 110){
                console.log("this.checkCard2 = true!!!!!!!");
                this.checkCard2 = true;
                this.maskNode2.active = false;
            }else{
                this.checkCard2 = false;
            }
            if(this.maskNode2.y-this.cardList[1].y < -230 || this.maskNode2.y-this.cardList[1].y > 230){
                console.log("this.checkCard2 = true!!!!!!!");
                this.checkCard2 = true;
                this.maskNode2.active = false;
            }
        }

        if(this.checkCard1 && this.checkCard2)
        {
            console.log("搓牌成功")
            if(this.canMove)
            {
                // if(this.showCardFunc2 != -1)
                    // this.unschedule(this.showCardFunc2);
                window.protectedUnschedule(this,this.showCardFunc2);
                this.btnOpenCard.active = false;
                this.showCardFunc2 = function(){
                    this.btnShowCardClickB();
                };
                this.scheduleOnce(this.showCardFunc2,0.5);
            }
            this.canMove = false;
        }else{
            console.log("搓牌失败,距离再拉大一些");
        }
    },

    showLayerB:function(handCard){//moveType:0:5张牌,明3张;1:5张牌,明4张;2:3张牌,明2张;3:3张牌,明0张;
        if(this.isInit == false)
            this.onInit();
        // if(this.showCardFunc != -1)
        //     this.unschedule(this.showCardFunc);
        // if(this.showCardFunc2 != -1)
        //     this.unschedule(this.showCardFunc2);
        window.protectedUnschedule(this,this.showCardFunc);
        window.protectedUnschedule(this,this.showCardFunc2);
        this.initCardB(handCard);
        this.node.active = true;
    },

    hideLayerB:function(){
        this.resetCardB();
        this.node.active = false;
    },

    resetCardB:function(){
        this.cardList[0].stopAllActions();
        this.cardList[1].stopAllActions();
        this.maskNode1.stopAllActions();
        this.maskNode2.stopAllActions();
        this.mask.stopAllActions();

        this.cardList[0].x = this.cardPos1.x;
        this.cardList[1].x = this.cardPos2.x;
        this.cardList[0].y = -700;
        this.cardList[1].y = -700;
        this.maskNode1.x = -128;
        this.maskNode1.y = -698;
        this.maskNode2.x = 130;
        this.maskNode2.y = -698;

        this.cardList[0].active = false;
        this.cardList[1].active = false;
        
        this.canMove = true;
        this.mask.opacity = 0;

        this.btnOpenCard.active = true;
        this.checkCard1 = false;
        this.checkCard2 = false;
    },

    initCardB:function(handCard){
        this.canMove = true;
        this.mask.runAction(cc.fadeTo(0.3,150));
        for(var i in handCard)
        {
            this.cardList[i].active = true;
            var curCardIndex = handCard[i].num;
            this.cardList[i].getComponent("cc.Sprite").spriteFrame = confige.paiGowCardFrameMap[curCardIndex];
        }
        this.maskNode2.active = true
        this.maskNode1.active = true
        this.cardList[0].runAction(cc.moveTo(1,cc.p(this.cardPos1.x,this.cardPos1.y)).easing(cc.easeOut(1.5)));
        this.cardList[1].runAction(cc.moveTo(1,cc.p(this.cardPos2.x,this.cardPos2.y)).easing(cc.easeOut(1.5)));
        this.maskNode1.runAction(cc.moveTo(1,cc.p(-128,2)).easing(cc.easeOut(1.5)));
        this.maskNode2.runAction(cc.moveTo(1,cc.p(130,2)).easing(cc.easeOut(1.5)));
    },

    btnShowCardClickB:function(){
        this.canMove = false;
        // if(this.showCardFunc != -1)
            // this.unschedule(this.showCardFunc);
        window.protectedUnschedule(this,this.showCardFunc);
        this.btnOpenCard.active = false;
        this.cardList[0].stopAllActions();
        this.cardList[1].stopAllActions();
        this.mask.stopAllActions();

        this.mask.runAction(cc.sequence(cc.delayTime(1),cc.fadeOut(0.75)));
        this.cardList[0].runAction(cc.sequence(cc.delayTime(0.5),cc.moveBy(1,cc.p(0,-700)).easing(cc.easeOut(2))));
        this.cardList[1].runAction(cc.sequence(cc.delayTime(0.5),cc.moveBy(1,cc.p(0,-700)).easing(cc.easeOut(2))));
        this.maskNode1.runAction(cc.sequence(cc.delayTime(0.5),cc.moveBy(1,cc.p(0,-700)).easing(cc.easeOut(2))));
        this.maskNode2.runAction(cc.sequence(cc.delayTime(0.5),cc.moveBy(1,cc.p(0,-700)).easing(cc.easeOut(2))));

        this.showCardFunc = function(){
            this.parent.btnShowCardOnMove();
        };
        this.scheduleOnce(this.showCardFunc,2);
    },
});
